﻿using LD.Host.Screw;
using System;
using System.Reflection;
using System.Windows.Forms;

namespace LD
{
    public partial class FrmWelcome : Form
    {
        private System.Timers.Timer m_Timer;
        private AppRoot appRoot = new AppRoot();
        public FrmWelcome()
        {
            InitializeComponent();
            Shown += FrmWelcome_Shown;
            m_Timer = new System.Timers.Timer(100);

        }

        private void FrmWelcome_Shown(object sender, EventArgs e)
        {
            m_Timer.Elapsed += M_Timer_Elapsed;
            m_Timer.AutoReset = false;
            m_Timer.Start();
        }

        private void M_Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //初始化参数
            ProgressUpdate(5);
            TextUpdate("加载缓存");
            appRoot.LoadCache();
            ProgressUpdate(10);
            appRoot.LoadPlcAdr();

            TextUpdate("加载机种信息");
            appRoot.LoadPartNo();
            ProgressUpdate(20);
            ProgressUpdate(30);

            TextUpdate("加载首页界面");
            ProgressUpdate(40);

            TextUpdate("TcpIp初始化");
            TcpIpService.Instance.Load();
            TcpIpService.Instance.Init();
            ProgressUpdate(60);
            TextUpdate("电批初始化");
            appRoot.Start();
            ProgressUpdate(100);
            Loading = true;
        }

        private void TextUpdate(string msg)
        {
            this.Invoke(new Action(() =>
                lab_loadText.Text = msg
            ));
        }
        private void ProgressUpdate(int val)
        {
            this.Invoke(new Action(() =>
                progressBarY1.Value = val
            ));
        }
        public bool Loading { get; private set; }

    }
}
